package com.tiancheng.trade.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.payment.enums.ChannelRefundTypeEnum;
import com.tiancheng.trade.payment.enums.FlowStatusEnum;
import com.tiancheng.trade.payment.mapper.IntRefundFlowDetailMapper;
import com.tiancheng.trade.payment.model.IntRefundFlowDetail;
import com.tiancheng.trade.payment.service.IntRefundFlowDetailService;
import com.tiancheng.trade.payment.vo.admin.res.PaymentRefundFlowDetailResVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


/**
英文版支付退款
 */
@Service
public class IntRefundFlowDetailServiceImpl implements IntRefundFlowDetailService {

    @Autowired
    private IntRefundFlowDetailMapper intRefundFlowDetailMapper;

    /**
     * 批量插入
     *
     * @param details {@link List<IntRefundFlowDetail>}
     * @return {@link Integer}
     */
    @Override
    public Integer saveBatch(List<IntRefundFlowDetail> details) {
        return intRefundFlowDetailMapper.saveBatch(details);
    }

    @Override
    public List<IntRefundFlowDetail> findByFlowId(String flowId) {
        if (StringUtils.isEmpty(flowId)) {
            return Collections.emptyList();
        }
        QueryWrapper<IntRefundFlowDetail> conditions = new QueryWrapper<>();
        conditions.eq("flow_id", flowId);
        return intRefundFlowDetailMapper.selectList(conditions);
    }

    @Override
    public List<PaymentRefundFlowDetailResVo> findSettleByFlowId(String flowId) {
      /*  List<IntRefundFlowDetail> intRefundFlowDetails = this.findByFlowId(flowId);
        if (CollectionUtils.isEmpty(intRefundFlowDetails)) {
            return Collections.emptyList();
        }
        List<Map<String, Object>> settleDetails = settleMatchRefundService.findByRefundFlowIds(Collections.singletonList(flowId), SettleStatusEnum.SUCCESS.getCode());
        List<PaymentRefundFlowDetailResVo> result = new ArrayList<>();
        for (IntRefundFlowDetail intRefundFlowDetail : intRefundFlowDetails) {
            PaymentRefundFlowDetailResVo p = ObjectConvertUtil.convert(intRefundFlowDetail,
                    PaymentRefundFlowDetailResVo.class).get();
            if (CollectionUtils.isEmpty(settleDetails)) {
                result.add(p);
                continue;
            }
            List<Map<String, Object>> subOrderSettleMatch = settleDetails.stream().filter(s ->
                    s.get("refund_flow_id").toString().equals(paymentRefundFlowDetail.getFlowId())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(subOrderSettleMatch) || subOrderSettleMatch.get(0).get("update_time") == null) {
                result.add(p);
                continue;
            }
            LocalDateTime updateTime = Instant.ofEpochMilli(((Timestamp) subOrderSettleMatch.get(0).get("update_time")).getTime()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            p.setApprSuccessDate(updateTime);
            p.setApprId(subOrderSettleMatch.get(0).get("appr_id").toString());
            p.setApprId(subOrderSettleMatch.get(0).get("dist_id").toString());
            result.add(p);
        }
        return result;*/
        return  null;
    }

    @Override
    public Integer updateSettleBatch(Collection<Long> ids, HashMap<String, Object> colValMap) {
        return intRefundFlowDetailMapper.updateSettleBatch(ids, colValMap);
    }

    @Override
    public Integer updateRefundStatus(String flowId, String subOrderId, ChannelRefundTypeEnum refundType, FlowStatusEnum flowStatusEnum) {
        QueryWrapper<IntRefundFlowDetail> conditions = new QueryWrapper<>();
        conditions.eq("flow_id", flowId);
        if(refundType.getCode().equals(ChannelRefundTypeEnum.SPLIT.getCode())){
            Assert.isTrue(StringUtils.isNotEmpty(subOrderId), "未查询到相应退款流水");
            // 部分退款
            conditions.eq("sub_order_id", subOrderId);
        }
        IntRefundFlowDetail entity = new IntRefundFlowDetail();
        entity.setRefundStatus(flowStatusEnum.getCode());
        return intRefundFlowDetailMapper.update(entity,conditions);
    }

    @Override
    public IntRefundFlowDetail findBySubFlowId(String subFlowId) {
        QueryWrapper<IntRefundFlowDetail> conditions = new QueryWrapper<>();
        conditions.eq("sub_flow_id", subFlowId);
        return intRefundFlowDetailMapper.selectOne(conditions);
    }

    @Override
    public List<IntRefundFlowDetail> selectList(QueryWrapper<IntRefundFlowDetail> condition) {
        return intRefundFlowDetailMapper.selectList(condition);
    }
}
